package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.alibaba.fastjson.JSONObject;
import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityWeatherBinding;
import com.apex.aolonfitdemo.dialog.CurrentWeatherTemperatureDialog;
import com.apex.aolonfitdemo.dialog.UvDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.aolonfitdemo.dialog.WeatherDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.WeatherCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleWeather;
import com.cyber.bridge.module.bean.FutureHourWeatherEntity;
import com.cyber.bridge.module.bean.FutureWeatherEntity;
import com.cyber.bridge.module.bean.TodayWeatherEntity;
import com.cyber.bridge.module.listener.OnCyberPushWeatherListener;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class WeatherActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    SimpleDateFormat simpleDateFormat;
    private float cTemp, iTemp, aTemp, sTime = 6, dTime = 18, uv;

    private CurrentWeatherTemperatureDialog currentWeatherTemperatureDialog, minTempDialog, maxTempDialog;
    private WeatherDialog dayWeatherDialog, nightWeatherDialog;

    private UvDialog uvDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(WeatherActivity.this, getString(R.string.add_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityWeatherBinding activityWeatherBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityWeatherBinding = ActivityWeatherBinding.inflate(getLayoutInflater());
        setContentView(activityWeatherBinding.getRoot());
        activityWeatherBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityWeatherBinding.tool);
        getSupportActionBar().setTitle(null);
        activityWeatherBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cTemp = 10;
        iTemp = 5;
        aTemp = 20;
        activityWeatherBinding.sunriseTime.setText(sTime + "");
        activityWeatherBinding.sunsetTime.setText(dTime + "");
        AolonFitManager.getInstance().setOnPushWeatherListener(new OnCyberPushWeatherListener() {
            @Override
            public void pushWeather(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }
        });
        activityWeatherBinding.currentTemperature.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentWeatherTemperatureDialog == null) {
                    currentWeatherTemperatureDialog = new CurrentWeatherTemperatureDialog(WeatherActivity.this);
                    currentWeatherTemperatureDialog.setSelectListener(new CurrentWeatherTemperatureDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.currentTemperature.setText(sex);
                            cTemp = Float.parseFloat(sex);
                        }
                    });
                }
                if (!currentWeatherTemperatureDialog.isShowing()) {
                    currentWeatherTemperatureDialog.show();
                }
            }
        });
        activityWeatherBinding.daytimeWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dayWeatherDialog == null) {
                    dayWeatherDialog = new WeatherDialog(WeatherActivity.this);
                    dayWeatherDialog.setSelectListener(new WeatherDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.daytimeWeather.setText(sex);
                        }
                    });
                }
                if (!dayWeatherDialog.isShowing()) {
                    dayWeatherDialog.show();
                }


            }
        });
        activityWeatherBinding.minTemperature.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (minTempDialog == null) {
                    minTempDialog = new CurrentWeatherTemperatureDialog(WeatherActivity.this);
                    minTempDialog.setSelectListener(new CurrentWeatherTemperatureDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.minTemperature.setText(sex);
                            iTemp = Float.parseFloat(sex);

                        }
                    });
                }
                if (!minTempDialog.isShowing()) {
                    minTempDialog.show();
                }
            }
        });
        activityWeatherBinding.maxTemperature.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (maxTempDialog == null) {
                    maxTempDialog = new CurrentWeatherTemperatureDialog(WeatherActivity.this);
                    maxTempDialog.setSelectListener(new CurrentWeatherTemperatureDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.maxTemperature.setText(sex);
                            aTemp = Float.parseFloat(sex);
                        }
                    });
                }
                if (!maxTempDialog.isShowing()) {
                    maxTempDialog.show();
                }
            }
        });
        activityWeatherBinding.uvIntensity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (uvDialog == null) {
                    uvDialog = new UvDialog(WeatherActivity.this);
                    uvDialog.setSelectListener(new UvDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            activityWeatherBinding.uvIntensity.setText(sex + "");
                            uv = sex;
                        }
                    });
                }
                if (!uvDialog.isShowing()) {
                    uvDialog.show();
                }
            }
        });
        activityWeatherBinding.airQuality.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        activityWeatherBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String dWeather = activityWeatherBinding.daytimeWeather.getText().toString();
                if (TextUtils.isEmpty(dWeather)) {
                    return;
                }
                int type = 8;
                if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_clear))) {
                    type = 5;
                } else if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_cloudy))) {
                    type = 0;
                } else if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_fog))) {
                    type = 1;
                } else if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_gloomy))) {
                    type = 2;
                } else if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_rain))) {
                    type = 3;
                } else if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_snow))) {
                    type = 4;
                } else if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_sandstorm))) {
                    type = 6;
                } else if (dWeather.equalsIgnoreCase(getString(R.string.weather_type_haze))) {
                    type = 7;
                } else {
                    type = 8;
                }
                String air = activityWeatherBinding.airQuality.getText().toString();
                int aQ = 1;
                if (getString(R.string.weather_air_good).equalsIgnoreCase(air)) {
                    aQ = 1;
                }
                List<FutureWeatherEntity> entityList = new ArrayList<>();
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                entityList.add(new FutureWeatherEntity(12, 25, 3, 1, 90, calendar.getTimeInMillis()));

                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(WeatherActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                AolonFitManager.getInstance().pushWeather(new TodayWeatherEntity(activityWeatherBinding.place.getText().toString(), (int) cTemp, type, (int) sTime, (int) dTime, (int) aTemp, (int) iTemp, (int) uv, 80, 0, System.currentTimeMillis(), aQ), entityList, null);


            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        simpleDateFormat = null;
        if (currentWeatherTemperatureDialog != null) {
            currentWeatherTemperatureDialog.destroyDialog();
            currentWeatherTemperatureDialog = null;
        }
        if (minTempDialog != null) {
            minTempDialog.destroyDialog();
            minTempDialog = null;
        }
        if (maxTempDialog != null) {
            maxTempDialog.destroyDialog();
            maxTempDialog = null;
        }
        if (dayWeatherDialog != null) {
            dayWeatherDialog.destroyDialog();
            dayWeatherDialog = null;
        }
        if (nightWeatherDialog != null) {
            nightWeatherDialog.destroyDialog();
            nightWeatherDialog = null;
        }
        if (uvDialog != null) {
            uvDialog.destroyDialog();
            uvDialog = null;
        }

        super.onDestroy();
    }
    /**

     class AdapterWeather extends RecyclerView.Adapter<AdapterWeather.HoldView> {
    @NonNull
    @Override public HoldView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(WeatherActivity.this).inflate(R.layout.adapter_weather_recyclerview, parent, false);
    return new HoldView(itemView);
    }

    @Override public void onBindViewHolder(@NonNull HoldView holder, int position) {
    if (eaBleWeather == null || eaBleWeather.getS_day() == null || eaBleWeather.getS_day().isEmpty()) {
    return;
    }
    EABleWeather.EABleWeatherItem weatherItem = eaBleWeather.getS_day().get(position);
    EABleWeather.WeatherType dayType = weatherItem.getE_day_type();
    if (dayType != null) {
    if (dayType == EABleWeather.WeatherType.clear) {
    holder.dayText.setText(getString(R.string.weather_type_clear));

    } else if (dayType == EABleWeather.WeatherType.cloudy) {
    holder.dayText.setText(getString(R.string.weather_type_cloudy));
    } else if (dayType == EABleWeather.WeatherType.gloomy) {
    holder.dayText.setText(getString(R.string.weather_type_gloomy));
    } else if (dayType == EABleWeather.WeatherType.drizzle) {
    holder.dayText.setText(getString(R.string.weather_type_drizzle));
    } else if (dayType == EABleWeather.WeatherType.moderate_rain) {
    holder.dayText.setText(getString(R.string.weather_type_moderate_rain));
    } else if (dayType == EABleWeather.WeatherType.thunderstorm) {
    holder.dayText.setText(getString(R.string.weather_type_thunderstorm));
    } else if (dayType == EABleWeather.WeatherType.heavy_rain) {
    holder.dayText.setText(getString(R.string.weather_type_heavy_rain));
    } else if (dayType == EABleWeather.WeatherType.sleet) {
    holder.dayText.setText(getString(R.string.weather_type_heavy_sleet));
    } else if (dayType == EABleWeather.WeatherType.light_snow) {
    holder.dayText.setText(getString(R.string.weather_type_light_snow));
    } else if (dayType == EABleWeather.WeatherType.moderate_snow) {
    holder.dayText.setText(getString(R.string.weather_type_moderate_snow));
    } else if (dayType == EABleWeather.WeatherType.heavy_snow) {
    holder.dayText.setText(getString(R.string.weather_type_heavy_snow));
    }
    }
    EABleWeather.WeatherType nightType = weatherItem.getE_night_type();
    if (nightType != null) {
    if (nightType == EABleWeather.WeatherType.clear) {
    holder.nightText.setText(getString(R.string.weather_type_clear));
    } else if (nightType == EABleWeather.WeatherType.cloudy) {
    holder.nightText.setText(getString(R.string.weather_type_cloudy));
    } else if (nightType == EABleWeather.WeatherType.gloomy) {
    holder.nightText.setText(getString(R.string.weather_type_gloomy));
    } else if (nightType == EABleWeather.WeatherType.drizzle) {
    holder.nightText.setText(getString(R.string.weather_type_drizzle));
    } else if (nightType == EABleWeather.WeatherType.moderate_rain) {
    holder.nightText.setText(getString(R.string.weather_type_moderate_rain));
    } else if (nightType == EABleWeather.WeatherType.thunderstorm) {
    holder.nightText.setText(getString(R.string.weather_type_thunderstorm));
    } else if (nightType == EABleWeather.WeatherType.heavy_rain) {
    holder.nightText.setText(getString(R.string.weather_type_heavy_rain));
    } else if (nightType == EABleWeather.WeatherType.sleet) {
    holder.nightText.setText(getString(R.string.weather_type_heavy_sleet));
    } else if (nightType == EABleWeather.WeatherType.light_snow) {
    holder.nightText.setText(getString(R.string.weather_type_light_snow));
    } else if (nightType == EABleWeather.WeatherType.moderate_snow) {
    holder.nightText.setText(getString(R.string.weather_type_moderate_snow));
    } else if (nightType == EABleWeather.WeatherType.heavy_snow) {
    holder.nightText.setText(getString(R.string.weather_type_heavy_snow));
    }
    }
    EABleWeather.AirQuality airQuality = weatherItem.getE_air();
    if (airQuality != null) {
    if (airQuality == EABleWeather.AirQuality.excellent) {
    holder.qualityText.setText(getString(R.string.weather_air_excellent));
    } else if (airQuality == EABleWeather.AirQuality.good) {
    holder.qualityText.setText(getString(R.string.weather_air_good));
    } else if (airQuality == EABleWeather.AirQuality.bad) {
    holder.qualityText.setText(getString(R.string.weather_air_bad));
    }
    }
    EABleWeather.RaysLevel raysLevel = weatherItem.getE_rays();
    if (raysLevel != null) {
    if (raysLevel == EABleWeather.RaysLevel.weak) {
    holder.uvText.setText(getString(R.string.weather_rays_weak));
    } else if (raysLevel == EABleWeather.RaysLevel.medium) {
    holder.uvText.setText(getString(R.string.weather_rays_medium));
    } else if (raysLevel == EABleWeather.RaysLevel.strong) {
    holder.uvText.setText(getString(R.string.weather_rays_strong));
    } else if (raysLevel == EABleWeather.RaysLevel.very_strong) {
    holder.uvText.setText(getString(R.string.weather_rays_very_strong));
    } else if (raysLevel == EABleWeather.RaysLevel.super_strong) {
    holder.uvText.setText(getString(R.string.weather_rays_super_strong));
    }
    }
    EABleWeather.Moon moon = weatherItem.getE_moon();
    if (moon != null) {
    if (moon == EABleWeather.Moon.new_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_new_moon));
    } else if (moon == EABleWeather.Moon.waxing_crescent_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waxing_crescent_moon));
    } else if (moon == EABleWeather.Moon.quarter_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_quarter_moon));
    } else if (moon == EABleWeather.Moon.half_moon_1) {
    holder.moonText.setText(getString(R.string.weather_moon_half_moon_1));
    } else if (moon == EABleWeather.Moon.waxing_gibbous_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waxing_gibbous_moon));
    } else if (moon == EABleWeather.Moon.full_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_full_moon));
    } else if (moon == EABleWeather.Moon.waning_gibbous_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waning_gibbous_moon));
    } else if (moon == EABleWeather.Moon.half_moon_2) {
    holder.moonText.setText(getString(R.string.weather_moon_half_moon_2));
    } else if (moon == EABleWeather.Moon.last_quarter_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_last_quarter_moon));
    } else if (moon == EABleWeather.Moon.waning_crescent_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waning_crescent_moon));
    }
    }
    holder.minTempText.setText(weatherItem.getMin_temperature() + "");
    holder.maxTempText.setText(weatherItem.getMax_temperature() + "");
    calendar = Calendar.getInstance();
    calendar.setTimeInMillis(weatherItem.getSunrise_timestamp() * 1000);
    holder.sunriseText.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR) + " " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE));
    calendar.clear();
    calendar.setTimeInMillis(weatherItem.getSunset_timestamp() * 1000);
    Log.e(TAG, "日落时间:" + weatherItem.getSunrise_timestamp() * 1000);
    holder.sunsetText.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR) + " " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE));
    holder.minWindText.setText(weatherItem.getMin_wind_power() + "");
    holder.maxWindText.setText(weatherItem.getMax_wind_power() + "");
    holder.humidityText.setText(weatherItem.getAir_humidity() + "");
    holder.cloudinessText.setText(weatherItem.getCloudiness() + "");


    }

    @Override public int getItemCount() {
    return eaBleWeather == null ? 0 : (eaBleWeather.getS_day() == null ? 0 : eaBleWeather.getS_day().size());
    }

    class HoldView extends RecyclerView.ViewHolder {
    @BindView(R.id.daytime_weather) TextView dayText;
    @BindView(R.id.night_weather) TextView nightText;
    @BindView(R.id.min_temperature) TextView minTempText;
    @BindView(R.id.max_temperature) TextView maxTempText;
    @BindView(R.id.sunrise_time) TextView sunriseText;
    @BindView(R.id.sunset_time) TextView sunsetText;
    @BindView(R.id.air_quality) TextView qualityText;
    @BindView(R.id.min_wind_force) TextView minWindText;
    @BindView(R.id.max_wind_force) TextView maxWindText;
    @BindView(R.id.uv_intensity) TextView uvText;
    @BindView(R.id.air_humidity) TextView humidityText;
    @BindView(R.id.moon) TextView moonText;
    @BindView(R.id.cloudiness) TextView cloudinessText;


    public HoldView(@NonNull View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
    }
    }
    }
     */
}
